package com.huafeng.enums;

import com.huafeng.function.RequestFunctions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author 花风
 * @date 2023/4/6 21:38
 * @Description 请求类型枚举
 */
public enum RequestEnum {
    Friend("friend", RequestFunctions::Friend),
    Group("group",RequestFunctions::Group);

    private final String type;
    private final Function<List<String>,Object> function;

    private static final Map<String,Function<List<String>,Object>> map = new HashMap<>();
    static {
        RequestEnum[] requestEnums = RequestEnum.values();
        for (RequestEnum requestEnum : requestEnums) {
            map.putIfAbsent(requestEnum.type,requestEnum.function);
        }
    }

    public static Function<List<String>,Object> get(String type){
        Function<List<String>, Object> function = map.get(type);
        if (function == null){
            throw new NullPointerException("function is empty");
        }
        return function;
    }


    RequestEnum(String type,Function<List<String>,Object> function){
        this.type = type;
        this.function = function;
    }
}
